#ifndef _ASYM_H_
#define _ASYM_H_

#include "../CCP907_SEC_MODULE/INCLUDE/jr.h"
#define MAGIC 'Q'
#define IOCTL_TEST_ONE	_IO(MAGIC,23)
#define IOCTL_TEST_TWO	_IO(MAGIC,24)
#define IOCTL_TEST_TRE	_IO(MAGIC,25)

#define CDEVDEMO_MAJOR 255

#define BYTES_PER_MB				1048576
//#define USEC_PER_SEC				1000000
#define BITS_PER_BYTE				8

#define SYM_IV_LEN				0x10
#define SYM_KEY_LEN				0x10
#define SM1_KEY_LEN				0x20

#define ALG_SM1					0x80
#define ALG_SM4					0x90
#define AAI_CBC					0x10

#define ALG_DEC					0x0
#define ALG_ENC					0x1
#define INIT_FIN				0x3

#define SYM_ALG					0

#define USER_MEM				0

#define ONE_MB_DLEN				0x100000
#define OP5_KB_DLEN				0x100000

#define SYM_TEST_CNT				1

#define MULTI_THREAD_TEST			1
#define SINGLE_THREAD_TEST			0

#define	TEST_ASYM_SM2				1	
#define	TEST_ASYM_RSA				1	
#define	TEST_ASYM_RNG				1
#define	TEST_ASYM_SM9				0

/* ASYN_RESOURCE_RECOVERY: 
 * Enable asynchronous logic. 
 */
#if 0
#define ASYN_RESOURCE_RECOVERY
#endif
/* ASYN_MODE_BENCHMARK:
 * Measure the sym&hash benchmark speed number under
 * async mode. If disable this macro, the enqueue part and callback function of dequeue work separately(async mode).
 */
#if 0
#define ASYN_MODE_BENCHMARK
#endif

#ifndef ASYN_RESOURCE_RECOVERY
#define THREADS_NUM				12	
#define LOOP_NUM				4
#else
#define THREADS_NUM				64
#define LOOP_NUM				10
#endif

#define ASYN_OUTPUT_DUMP			0


#ifdef ASYN_RESOURCE_RECOVERY
#define csec_debug3 printk
#else
#define csec_debug3 noprintk
#endif

//#define csec_debug printk
#define csec_debug  noprintk
//#define csec_debug2 printk
#define csec_debug2 noprintk
//#define csec_debug4 printk
#define csec_debug4 noprintk
//#define csec_error printk
#define csec_error noprintk

extern unsigned char sm2_fp_param1[224];
extern struct completion sym_comp[THREADS_NUM];

void get_random_bytes(void *buf, size_t nbytes);
void inline_cnstr_jobdesc_sm2_genkey(unsigned int *desc, struct sm2_genkey_private_dma *para);
void inline_cnstr_jobdesc_sm2_encrypt(unsigned int *desc, struct sm2_enc_private_dma *para);
void inline_cnstr_jobdesc_sm2_decrypt(unsigned int *desc, struct sm2_dec_private_dma *para);
void inline_cnstr_jobdesc_sm2_signature(unsigned int *desc, struct sm2_sig_private_dma *para);
void inline_cnstr_jobdesc_sm2_verify(unsigned int *desc, struct sm2_ver_private_dma *para);
void inline_cnstr_jobdesc_sm2_exchange(unsigned int *desc, struct sm2_exc_private_dma *para);
void inline_cnstr_jobdesc_rsa_genkey_simplified(unsigned int *desc, struct rsa_genkey_dma *para);
void inline_cnstr_jobdesc_rsa_pub_priv_simplified(unsigned int *desc, struct rsa_pub_priv_dma *para);
void inline_cnstr_jobdesc_rsa_priv_crt_simplified(unsigned int *desc, struct rsa_pub_priv_dma *para);
void inline_cnstr_jobdesc_rng_simplified(unsigned int *desc, struct rng_para_dma *para);
void inline_cnstr_jobdesc_sm9_genkey_for_enc(unsigned int *desc, struct sm9_genkey_for_enc_para *para);
void inline_cnstr_jobdesc_sm9_genkey_for_sig(unsigned int *desc, struct sm9_genkey_for_sig_para *para);
void inline_cnstr_jobdesc_sm9_genkey_for_exc(unsigned int *desc, struct sm9_genkey_for_exc_para *para);
void inline_cnstr_jobdesc_sm9_encrypt(unsigned int *desc, struct sm9_enc_para *para);
void inline_cnstr_jobdesc_sm9_decrypt(unsigned int *desc, struct sm9_dec_para *para);
void inline_cnstr_jobdesc_sm9_signature(unsigned int *desc, struct sm9_sig_para *para);
void inline_cnstr_jobdesc_sm9_verify(unsigned int *desc, struct sm9_ver_para *para);
void inline_cnstr_jobdesc_sm9_exchange(unsigned int *desc, struct sm9_exc_para *para);

struct csec_priv_t* cards_enqueue_pre(struct ccore_cards_t *ccore_cards);
int cards_enqueue(struct csec_priv_t *csec_priv,uint32_t *desc_addr,dma_addr_t desc_phy_addr,
        void (*callback)(struct csec_priv_t *csec_priv_s,void *desc_addr_s,dma_addr_t desc_phy_addr_s,uint32_t status_s, void *arg_s),void *arg);
struct ccore_cards_t *get_ccore_cards(void);

void sm2_multi_test_asyn(void);
void noprintk(const char *fmt,...);

#if 1
struct sym_thread_st
{
	unsigned char *iv;
	unsigned char *key;
	unsigned int data_len;
	unsigned char *input_data;
	unsigned char *output_data;
};
#endif

struct vir_dma_st
{
	void *p;
	dma_addr_t dma;
};

enum asym_algorithm {
	SM2	= 0x10,
	SM9	= 0x11,
	RSA1024 = 0x12,
	RSA2048 = 0x13
};

enum asym_crypto_type {
	GENKEY	= 0x20,
	ENCRYPT = 0x21,
	DECRYPT = 0x22,
	SIGN	= 0x23,
	VERIFY	= 0x24,
	EXCH	= 0x25
};

struct sm2_genkey_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pub_key;
	uint8_t *pri_key;
	dma_addr_t fp256_param_dma;
	dma_addr_t pub_key_dma;
	dma_addr_t pri_key_dma;
	uint32_t fp256_param_len;
	uint32_t pub_key_len;
	uint32_t pri_key_len;
};
struct sm2_enc_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pub_key;
	uint8_t *msg;
	uint8_t *c;
	uint8_t *hashin;
	uint8_t *k;
	uint32_t *ct;
	dma_addr_t fp256_param_dma;
	dma_addr_t pub_key_dma;
	dma_addr_t msg_dma;
	dma_addr_t c_dma;
	dma_addr_t hashin_dma;
	dma_addr_t k_dma;
	dma_addr_t ct_dma;
	uint32_t fp256_param_len;
	uint32_t pub_key_len;
	uint32_t msg_len;
	uint32_t c_len;
	uint32_t hashin_len;
	uint32_t k_len;
	uint32_t ct_len;
};
struct sm2_dec_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pri_key;
	uint8_t *msg;
	uint8_t *c;
	uint8_t *hashin;
	uint32_t *ct;
	dma_addr_t fp256_param_dma;
	dma_addr_t pri_key_dma;
	dma_addr_t msg_dma;
	dma_addr_t c_dma;
	dma_addr_t hashin_dma;
	dma_addr_t ct_dma;
	uint32_t fp256_param_len;
	uint32_t pri_key_len;
	uint32_t msg_len;
	uint32_t c_len;
	uint32_t hashin_len;
	uint32_t ct_len;

};
struct sm2_sign_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pri_key;
	uint8_t *msg;
	uint8_t *r;
	uint8_t *s;
	uint8_t *one;
	uint8_t *k;
	uint8_t *z;
	dma_addr_t fp256_param_dma;
	dma_addr_t pri_key_dma;
	dma_addr_t msg_dma;
	dma_addr_t r_dma;
	dma_addr_t s_dma;
	dma_addr_t one_dma;
	dma_addr_t k_dma;
	dma_addr_t z_dma;
	uint32_t fp256_param_len;
	uint32_t pri_key_len;
	uint32_t msg_len;
	uint32_t r_len;
	uint32_t s_len;
	uint32_t one_len;
	uint32_t k_len;
	uint32_t z_len;
};
struct sm2_verify_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pub_key;
	uint8_t *msg;
	uint8_t *r;
	uint8_t *s;
	uint8_t *one;
	uint8_t *z;
	dma_addr_t fp256_param_dma;
	dma_addr_t pub_key_dma;
	dma_addr_t msg_dma;
	dma_addr_t r_dma;
	dma_addr_t s_dma;
	dma_addr_t one_dma;
	dma_addr_t z_dma;
	uint32_t fp256_param_len;
	uint32_t pub_key_len;
	uint32_t msg_len;
	uint32_t r_len;
	uint32_t s_len;
	uint32_t one_len;
	uint32_t z_len;
};
struct sm2_exch_res {
	/*ctrl_data: Output control data for contrast*/
	uint8_t *ctrl_data;

	uint8_t *fp256_param;
	uint8_t *pri_key;
	uint8_t *hashin;
	uint8_t *hashout;
	uint32_t *ct;
	uint8_t *one;
	uint8_t *pub_key1;
	uint8_t *tmp_pub_key;
	uint8_t *tmp_pri_key;
	uint8_t *tmp_pub_key1;
	uint8_t *x1;
	uint8_t *x2;
	uint8_t *z1;
	uint8_t *z2;
	uint8_t *za;
	uint8_t *zb;
	uint8_t *key;
	uint8_t *s1;
	uint8_t *s2;
	dma_addr_t fp256_param_dma;
	dma_addr_t pri_key_dma;
	dma_addr_t hashin_dma;
	dma_addr_t hashout_dma;
	dma_addr_t ct_dma;
	dma_addr_t one_dma;
	dma_addr_t pub_key1_dma;
	dma_addr_t tmp_pub_key_dma;
	dma_addr_t tmp_pri_key_dma;
	dma_addr_t tmp_pub_key1_dma;
	dma_addr_t x1_dma;
	dma_addr_t x2_dma;
	dma_addr_t z1_dma;
	dma_addr_t z2_dma;
	dma_addr_t za_dma;
	dma_addr_t zb_dma;
	dma_addr_t key_dma;
	dma_addr_t s1_dma;
	dma_addr_t s2_dma;
	uint32_t fp256_param_len;
	uint32_t pri_key_len;
	uint32_t hashin_len;
	uint32_t hashout_len;
	uint32_t ct_len;
	uint32_t one_len;
	uint32_t pub_key1_len;
	uint32_t tmp_pub_key_len;
	uint32_t tmp_pri_key_len;
	uint32_t tmp_pub_key1_len;
	uint32_t x1_len;
	uint32_t x2_len;
	uint32_t z1_len;
	uint32_t z2_len;
	uint32_t za_len;
	uint32_t zb_len;
	uint32_t key_len;
	uint32_t s1_len;
	uint32_t s2_len;
};


struct asym_resource {
	struct result result;
	struct completion *comp;
	void *callback;
	/* algs: SM2,SM9 or RSA */
	uint8_t algs;
	/* type: generate key, encrypt, decrypt,
	 * signature, verify or exchange.
	 */
	uint8_t type;
	
	union {
		/* SM2 resources */
		struct sm2_genkey_res	sm2_gk_res;
		struct sm2_enc_res	sm2_enc_res;
		struct sm2_dec_res	sm2_dec_res;
		struct sm2_sign_res	sm2_sign_res;
		struct sm2_verify_res	sm2_veri_res;
		struct sm2_exch_res	sm2_exch_res;
#if 0
		/* SM9 resources*/
		struct sm9_genkey_res	sm9_gk_res;
		struct sm9_enc_res	sm9_enc_res;
		struct sm9_dec_res	sm9_dec_res;
		struct sm9_sign_res	sm9_sign_res;
		struct sm9_verify_res	sm9_veri_res;
		struct sm9_exch_res	sm9_exch_res;
#endif
		/* RSA resources*/
	};
	
};

#endif
